﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;

namespace MyFramework.Common
{
    /// <summary>
    /// Base entity
    /// </summary>
    public abstract class Entity : IEntity
    {
        #region Events

        /// <summary>
        /// Occurs when a property value is changing.
        /// </summary>
        public event PropertyChangingEventHandler PropertyChanging;

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="Entity"/> class.
        /// </summary>
        public Entity()
        {

        }

        #endregion

        #region Properties



        #endregion

        #region Methods

        /// <summary>
        /// Raises the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">The name of the changed property.</param>
        public void RaisePropertyChangedEvent(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged.RaiseChanged(this, propertyName);
            }

        }

        /// <summary>
        /// Raises the property changed event.
        /// </summary>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="projection">The projection.</param>
        public virtual void RaisePropertyChangedEvent<TProperty>(Expression<Func<TProperty>> projection)
        {
            RaisePropertyChangedEvent(projection.GetPropertyName());
        }

        /// <summary>
        /// Raises the PropertyChanging event.
        /// </summary>
        /// <param name="propertyName">The name of the changing property.</param>
        public void RaisePropertyChangingEvent(string propertyName)
        {
            if (PropertyChanging != null)
            {
                this.PropertyChanging.RaiseChanging(this, propertyName);
            }
        }

        /// <summary>
        /// Raises the property changing event.
        /// </summary>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="projection">The projection.</param>
        public virtual void RaisePropertyChangingEvent<TProperty>(Expression<Func<TProperty>> projection)
        {
            RaisePropertyChangingEvent(projection.GetPropertyName());
        }

        #endregion

    }
}
